<!DOCTYPE html>
<html>
{include file='/common/_meta'}

<script type="text/javascript" src="__admin__/layui/layui.js"></script>

<body>
    <!-- 头部-->
{include file='/common/top'}
    <!-- /头部 -->
    
    <div class="main-container container-fluid">
        <div class="page-container">
                        <!-- Page Sidebar -->
        {include file='/common/left'}
            <!-- /Page Sidebar -->
            <!-- Page Content -->
            <div class="page-content">
                <!-- Page Breadcrumb -->
                <div class="page-breadcrumbs">
                    <ul class="breadcrumb">
                        <li>
                            <a href="{:url('Index/index')}">系统</a>
                        </li>
                                            <li>
                            <a href="">商品管理</a>
                        </li>
                        <li class="active">数据备份</li>
                    </ul>
                </div>
                <!-- /Page Breadcrumb -->

                <!-- Page Body -->
                <div class="page-body">

<script>
    layui.use(['jquery','layer'],function(){
      window.$ = layui.$;
      var layer = layui.layer;
      //备份表方法
      $("#export").click(function(){
          $(this).html("正在发送备份请求...");
          $.post(
            $("#export-form").attr("action"),
            $("#export-form").serialize(), 
            function(data){
               
              if(data.code==1){
                $("#export").html( "开始备份，请不要关闭本页面！");
                backup(data.data.tab);
                window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
              }else{
                 layer.tips(data.msg, "#export", {
                  tips: [1, '#3595CC'],
                  time: 4000
                });
                $("#export").html("立即备份");
              }
              
            }, "json");
            return false;  
      }); 
      //递归备份表
      function backup(tab,status){
        status && showmsg(tab.id, "开始备份...(0%)");
        $.get( $("#export-form").attr("action"), tab, function(data){
          // console.log(data)
                if(data.code==1){
                  showmsg(tab, data.msg);

                  if(!$.isPlainObject(data.data.tab)){
                    $("#export").html("备份完成");
                    window.onbeforeunload = function(){ return null }
                    return;
                  } 

                  backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                  $("#export").html("立即备份");
                }
            }, "json");

      }
    //修改备份状态
    function showmsg(tab, msg){
       $("table tbody tr").eq(tab.id).find(".infos").html(msg)
    }
   
     //优化表
      $("#optimize").click(function(){
           $.post(this.href, $("#export-form").serialize(), function(data){
           
            layer.tips(data.msg, "#optimize", {
              tips: [1, '#3595CC'],
              time: 4000
            });
    
            }, "json");
            return false;    
      });

      //修复表
      $("#repair").on("click",function(e){
         
          $.post(this.href, $("#export-form").serialize(), function(data){
            layer.tips(data.msg, "#repair", {
              tips: [1, '#3595CC'],
              time: 4000
            });
            }, "json");
            return false; 
      });
    });

  </script>

    <a id="export" class="btn btn-sm btn-azure btn-addon" href="javascript:;" autocomplete="off">立即备份</a>

    <a id="optimize" href="{:url('data/optimize')}" class="btn btn-sm btn-azure btn-addon">优化表</a>
    <a id="repair" href="{:url('data/repair')}" class="btn btn-sm btn-azure btn-addon">修复表</a>
    <a  href="{:url('data/importlist')}" class="btn btn-sm btn-azure btn-addon">还原数据库</a>
    <div class="row">
    <form id="export-form" method="post" action="{:url('data/export')}">
        <div class="col-lg-12 col-sm-12 col-xs-12">
            <div class="widget">
                <div class="widget-body">
                    <div class="flip-scroll">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th width="48" class="text-center">
                                        <label style="padding-right: 10px;">
                                            <input class="layuiss form-control" id="username" name="" checked="checked" value="" type="checkbox">
                                            <span class="text"></span>
                                        </label>
                                    </th>
                                    <th class="text-center">表名</th>
                                    <th class="text-center">数据量</th>
                                    <th class="text-center">数据大小</th>
                                    <th class="text-center">创建时间</th>
                                    <th class="text-center">备份状态</th>
                                    <th class="text-center">操作</th>
                                </tr> 
                            </thead>
    


                            <tbody>
                                {foreach name='list' item='table'}   
                                <tr>
                                    <td class="text-center">
                                        <label style="padding-right: 10px;">
                                            <input class="ids form-control" id="username" name="tables[]" checked="checked" value="{$table.name}" type="checkbox">
                                            <span class="text"></span>
                                        </label>
                                    </td>
                                    <td class="text-center">{$table.name}</td>
                                    <td class="text-center">{$table.rows}</td>
                                    <td class="text-center">{$table.data_length|format_bytes}</td>
                                    <td class="text-center">{$table.create_time}</td>
                                    <td class="infos text-center" width='200' style="color:#09f">未备份</td>
                                    <td class="text-center">
                                        <a  href="{:url('data/optimize',['tables'=>$table['name']])}" class="btn btn-sm btn-warning shiny">优化表</a>&nbsp;
                                        <a  href="{:url('data/repair',['tables'=>$table['name']])}" class="btn btn-primary btn-sm shiny">修复表</a>
                                    </td>
                                </tr>
                                {/foreach}
                            </tbody>


                        </table>
                    </div>
                </div>
            </div>
        </div>
        </form>
    </div>


                </div>
                <!-- /Page Body -->
            </div>
            <!-- /Page Content -->
        </div>  
    </div>

        <!--Basic Scripts-->
    {include file='/common/footer'}
    

    <script>

        $(".layuiss").click(function(){
            if($(this).is(':checked')){
                $('.ids').prop('checked','checked');             
            } else {
                $('.ids').prop('checked',false);             
            }
        })

    </script>

</body></html>